<?php
require_once  __DIR__ . '/includes/authenticate.php';

include_once './db_bb_connect.php';

$username = $_SESSION['username'];

require_once __DIR__ . '/includes/BasketbalFunctions.php';
$basketbalfunctions = new BasketbalFunctions();

$user_key = $basketbalfunctions->GetUserkeyFromUsername($username);
$teamID = $basketbalfunctions->GetCurrentTeamIDFromCoachKey($user_key);
$season = $basketbalfunctions->GetLastActiveSeasonFromCoachID($user_key);

?>

<!DOCTYPE html>
<html>
<head>
	<title>Add Game</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

 <div class="container-fluid">
<div class="row">
<div class="col-lg-3 col-md-3"></div>
<div class="col-lg-6 col-md-6"> 
<h4> Add Game </h4>

<form class="form-horizontal">

  <input type="hidden" name="coachteamID" value="<?= $teamID?>">
  <input type="hidden" name="season" value="<?= $season?>">

	<div class="form-group row">
        	<label class="control-label col-sm-4" for="awayteam">Opponent: </label>
          	<select class="form-control" name="opponentteamID">
         		<option value="" disabled="disabled" selected="selected">Select a team</option>
<?php

$sql = "SELECT teamID, teamname 
FROM teams_list
ORDER BY teamname";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){	

  $teamID=$row['teamID']; 
  $teamname=$row['teamname']; 

  echo "<option value='$teamID'>$teamname </option>";

}

  ?>
    		</select>

	</div>

  <div class="form-group row">
          <label class="control-label col-sm-4" for="awayteam">Location: </label>
            <select class="form-control" name="location">
              <option value="" disabled="disabled" selected="selected">Select a location</option>
              <option value="home">Home</option>
              <option value="away">Away</option>
          </select>
</div>

<div class="form-group row">
          <label class="control-label col-sm-4" for="awayteam">Game Type </label>
            <select class="form-control" name="gametype">
              <option value="" disabled="disabled" selected="selected">Select a gametype</option>
              <option value="league">League Game</option>
              <option value="practice">Practice Game</option>
          </select>
</div>

	<div class="form-group row">
		<label class="control-label col-sm-4" for="date">Date of Game: </label>
		<div class="col-sm-8">
			<input class="form-control" type="date" name="date" placeholder="yyyy-mm-dd" />	
		</div>
	</div>
	
	<div class="form-group">
		<div class="col-xs-offset-3 col-sm-offset-4 col-sm-3 col-md-offset-4 col-md-3">
		<button class="btn btn-success" id="submit" type="submit" name="submit">Add Game </button>
		<button type="reset" class="btn btn-danger ">Reset</button> 
		</div>
	</div>

</form>

<br><br>

<div id="responsemessage"></div>


</div>
<div class="col-lg-3 col-md-3"></div>
</div>
</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<script type="text/javascript">

$("#submit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./includes/handlers/admin_addgametoseason_handler.php",
        data: postData,
        success: function(data) { 
            $("#responsemessage").html(data); 
        },
        error: function (xhr, status, error) {
        	alert(postData);
        	$("#responsemessage").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>


</body>
</html>